#
# This file is part of the kernelstudio package.
#
# (c) 2014-2025 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.
from ksai.document.extractor.factory import extractor_factory
from ksai.models.document_attachment import DocumentContext
from ksai.persistence.storage.attachment import Attachment
from ksai.persistence.storage.events import app_storage_file_pre_saved, app_storage_file_post_persistence
from ksai.threads.document_processor import extractor_processor
from ksai.web.exceptions import UploadUnsupportedMimeException


@app_storage_file_pre_saved.connect
def post_check_supported_mimes(attachment: Attachment):
    if "labeler_document" == attachment.channel and attachment.mime not in extractor_factory.supported_mines:
        raise UploadUnsupportedMimeException()


@app_storage_file_post_persistence.connect
def post_persistence_document(context: DocumentContext):
    if "labeler_document" == context.attachment.channel:
        extractor_processor.put(context)
